﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Common
{
    public static class HtmlHelpers
    {
        static public string StripHtml(string html)
        {
            Regex objRegExp = new Regex("<(.|\n)+?>");

            string strOutput = objRegExp.Replace(html, " ");

            strOutput = strOutput.Replace("<", "&lt;");
            strOutput = strOutput.Replace(">", "&gt;");
            strOutput = strOutput.Replace("&nbsp;", "");

            Regex r = new Regex(@"\s+", RegexOptions.CultureInvariant);
            strOutput = r.Replace(strOutput, " ");

            return strOutput;
        }

        /// <summary>
        /// Returns rss web host
        /// </summary>
        static public string BaseUrl
        {
            get
            {
                HttpRequest req = HttpContext.Current.Request;
                string port = req.ServerVariables["SERVER_PORT"];
                port = (port == null || port.In("443", "80")) ? string.Empty : ":" + port;

                string protocol = req.ServerVariables["SERVER_PORT_SECURE"];
                protocol = (protocol == null || protocol == "0") ? "http://" : "https://";

                return protocol + req.ServerVariables["SERVER_NAME"] + port;
            }
        }
    }

    public static class DateExtensions
    {
        public static string ToRfc822(this DateTime dt)
        {
            string rfc822DateTime = dt.ToString(@"ddd, dd MMM yyyy HH:mm:ss zzzz (\U\T\C)",
                System.Globalization.DateTimeFormatInfo.InvariantInfo);
            rfc822DateTime = rfc822DateTime.Remove(rfc822DateTime.LastIndexOf(':'), 1);
            return rfc822DateTime;
        }

#if false
        public static string ToRfc822(this DateTime dt)
        {
            return String.Format("{0}, {1} {2} {3} {4}:{5}:{6} {7}",
                dt.DayOfWeek.ToString().Substring(0, 3), dt.Day, dt.GetMonth(),
                dt.Year, dt.Hour.ToString().PadLeft(2, '0'),
                dt.Minute.ToString().PadLeft(2, '0'),
                dt.Second.ToString().PadLeft(2, '0'), dt.GetTimeZone());
        }

        private static string GetMonth(this DateTime dt)
        {
            return new string[]{
                "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
            }
            [dt.Month - 1];
        }

        private static string GetTimeZone(this DateTime dt)
        {
#if FIX_TIME_ZONE
            StringBuilder builder = new StringBuilder();
            String[] tz = TimeZone.CurrentTimeZone.StandardName.Split(' ');
            foreach (string t in tz)
            {
                builder.Append(t.Substring(0, 1));
            }

            return builder.ToString();
#else
            return "-300";
#endif
        }
#endif

        public static string WithoutSpanishChars(this string s)
        {
            s = Regex.Replace(s, @"[^a-z0-9,.áéíóúüñÑ\s]", "", RegexOptions.IgnoreCase);
            s = s.ToLower();
            s = s.Replace('á', 'a')
                    .Replace('é', 'e')
                    .Replace('í', 'i')
                    .Replace('ó', 'o')
                    .Replace('ú', 'u')
                    .Replace('ü', 'u')
                    .Replace('ñ', 'n');

            return s;
        }
    }

    public static class StringUtil
    {
        public static string ToUrlString(this string s)
        {
            s = s.Trim();
            s = Regex.Replace(s, @"[\s]", "-", RegexOptions.IgnoreCase);
            s = System.Web.HttpUtility.HtmlEncode(s);
            s = Regex.Replace(s, @"[&;#]", "", RegexOptions.IgnoreCase);

            return s;
        }

        public static bool In(this string s, params string[] strArray )
        {
            foreach (string str in strArray)
            {
                if (s == str) return true;
            }

            return false;
        }

    }
}
